<?php
/**
 * 子应用入口
 * @param  array
 * @return [type]
 */
function run_post_app($uri = array()) {
	if (isset($uri[5]) && !empty($uri[5])) {
		$parameter = $uri[5];
		$parameter = str_replace('.html', '', $parameter);
		$parameter = str_replace('.htm', '', $parameter);
		post_show_page($parameter);
	} else {
		show_not_found();
	}
}

/**
 * 统计并显示统计数据
 * @param  string
 * @return [type]
 */
function post_show_count($hash = '') {
	if (empty($hash)) {
		return;
	}
	$hash = filter_data($hash);
	$ip = get_ip();
	$user_agent = $_SERVER['HTTP_USER_AGENT'];
	require_once APP_PATH . '/post/model.php';
	$ret = post_model_insert_count($hash, $ip, $user_agent);
	if (!$ret) {
		//同一个IP不允许重复记录
		//更新数据
		post_model_update_count_by_hash($hash);
	}
	//获取数据
	$page_view_count = post_model_get_count_by_hash($hash);

	//显示数据
	echo 'document.write("' . $page_view_count . '");';
}

/**
 * 显示post页面
 * @param  string
 * @return [type]
 */
function post_show_page($hash = '') {
	if (empty($hash)) {
		show_not_found();
		return;
	}
	$hash = filter_data($hash);
	// app start.
	if (is_file(POST_HTML_PATH . $hash . '.html') && (time() - filemtime(POST_HTML_PATH . $hash . '.html') < 1200) && !IS_DEBUG) {
		//缓存未失效则直接加载静态文件
		include POST_HTML_PATH . $hash . '.html';
	} else {
		require_once APP_PATH . '/post/build.php';
		$post_html_path = post_build_page_by_hash($hash);
		//输出
		if ($post_html_path) {
			include $post_html_path;
		} else {
			show_not_found();
		}
	}
}